package com.offcn.service;

import com.offcn.dao.CourseDao;
import com.offcn.dao.CourseUserDao;
import com.offcn.dao.UserDao;
import com.offcn.dao.daoimpl.CourseDaoImpl;
import com.offcn.dao.daoimpl.CourseUserDaoImpl;
import com.offcn.dao.daoimpl.UserDaoImpl;
import com.offcn.pojo.Course;
import com.offcn.pojo.CourseUser;
import com.offcn.pojo.User;
import com.offcn.util.PageUtil;

import java.util.List;

public class CourseUserService {
    private CourseUserDao courseUserDao = new CourseUserDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    private CourseDao courseDao = new CourseDaoImpl();

    public static void updateFlag(String orderNum) {
    }

    public PageUtil<CourseUser> queryAllByPage(String currentPage, String pageSize, String search) {
        List<Integer> list = userDao.queryAll(search);
        //根据用户的id查询选课信息
        int totalCount = courseUserDao.getCount(list);
        PageUtil<CourseUser> courseUserPageUtil = new PageUtil<>(totalCount, currentPage, Integer.valueOf(pageSize));
        //查询符合条件的分页数据
        List<CourseUser> courseUserList = courseUserDao.queryAllByPage(courseUserPageUtil, list);
        for (CourseUser courseUser : courseUserList) {
            //查询用户
            User user = userDao.queryUserById(courseUser.getUid());
            Course course = courseDao.queryUserById(courseUser.getCid());
            courseUser.setUser(user);
            courseUser.setCourse(course);
        }
        courseUserPageUtil.setList(courseUserList);
        return courseUserPageUtil;
    }

    public int updateCourse(CourseUser courseUser) {
        return courseUserDao.updateCourse(courseUser);
    }

    public int delAllCourse(String ids) {
        return courseUserDao.delAllCourse(ids);
    }

    public int insertCourseUser(Integer cid, Integer uid, String orderName) {

        return courseUserDao.insertCourseUser(cid,uid,orderName);
    }
}
